
package com.kugou.game.sdk.ui.dialog;

import com.kugou.framework.base.BaseDialog;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SetNickNameDialog extends BaseDialog {

    private Activity activity;

    private Button btnOk, btnCancel;

    private EditText etNickName;

    private ImageView ivClose;

    private LinearLayout layout;

    private TextView title;

    public SetNickNameDialog(Activity activity, int theme) {
        super(activity, theme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        this.activity = activity;
        initView();
    }

    public SetNickNameDialog(Activity activity) {
        this(activity, 0);
    }

    private void initView() {
        int layoutId = Rx.layout.kg_dialog_set_nickname;
        setContentView(layoutId);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();

        layout = (LinearLayout) findViewById(Rx.id.kg_dialog_layout);

        btnOk = (Button) findViewById(Rx.id.kg_common_dialog_btn_ok);
        if (btnOk == null) {
            throw new RuntimeException(
                    "Your content must have a ImageButton whose id attribute is "
                            + "'R.id.kg_common_dialog_btn_ok'");
        }
        btnCancel = (Button) findViewById(Rx.id.kg_common_dialog_btn_cancel);
        if (btnCancel == null) {
            throw new RuntimeException(
                    "Your content must have a ImageButton whose id attribute is "
                            + "'R.id.kg_common_dialog_btn_cancel'");
        }
        ivClose = (ImageView) findViewById(Rx.id.kg_iv_close);
        if (ivClose == null) {
            throw new RuntimeException("Your content must have a ImageView whose id attribute is "
                    + "'R.id.kg_iv_close'");
        }
        ivClose.setOnClickListener(mCloseClickListener);
        btnCancel.setOnClickListener(mCancelClickListener);
        title = (TextView) findViewById(Rx.id.kg_tv_dialog_title);
        etNickName = (EditText) findViewById(Rx.id.kg_et_user_nick_name);
        setOKBtnText("确定");
        setCancelText("取消");
    }

    private View.OnClickListener mCloseClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    private View.OnClickListener mCancelClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    /**
     * 设置关闭按钮的可见性
     * 
     * @param visibility
     */
    public void setCloseVisibility(int visibility) {
        ivClose.setVisibility(visibility);
    }

    public void setOnCloseClickListener(View.OnClickListener listener) {
        ivClose.setOnClickListener(listener);
    }

    public void setOKClickListener(View.OnClickListener listener) {
        btnOk.setOnClickListener(listener);
    }

    public void setCancelClickListener(View.OnClickListener listener) {
        btnCancel.setOnClickListener(listener);
    }

    /**
     * 设置对话框标题
     */
    public void setDialogTitle(String str) {
        title.setText(str);
    }

    /**
     * 设置取消按钮名称
     */
    public void setCancelText(String str) {
        btnCancel.setText(str);
    }

    /**
     * 设置ok按钮名称
     */
    public void setOKBtnText(String str) {
        btnOk.setText(str);
    }

    /**
     * 获取昵称输入框内容
     */
    public String getNickName() {
        return etNickName.getText().toString();
    }

    /**
     * 设置昵称输入框内容
     */
    public void setNickName(String string) {
        etNickName.setText(string);
    }

    public void hideSoftInput() {
        try {
            IBinder token = getCurrentFocus().getWindowToken();
            ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(token, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
